const { Tool, API, Event, Storage} = global.$util
Page({
  data: {
    name: '',
    shopList: [],
    show: false,
    shopName: '',
    isLoading: false,
    isMore: true,
    page: 1,
    tipVal: '', // 默认是无 取值 1 2 3
    currentPage: 1, // 当前的页数
    pageSize: 10, // 每次加载请求的条数 默认10
    params: {},
    isAjax: false 
  },
  onLoad: function (options) {
    this.getShopList()
  },
  getShopList() {
    let params = {}
    if (this.data.isAjax) return
      this.data.isAjax = true
      params = {
        ...this.data.params,
        pageNum: this.data.currentPage,
        pageSize: this.data.pageSize
      }
    if (this.data.name) params.name = this.data.name
    API.getShopList(params).then((res) => {
      let datas = res.data || {}
            if (datas.total > 0) {
                let isMore = Tool.hasNextPage(this.data.currentPage, this.data.pageSize, datas.total, datas.rows.length)
                this.setData({
                  shopList: res.data.rows || [],
                  tipVal: '',
                  isMore: isMore,
                }, ()=> {
                    this.data.loadMoreView = this.selectComponent(".loadMoreView")
                    this.data.loadMoreView.loadMoreComplete()
                })
            } else if (!datas.total) {
                this.setData({
                    tipVal: 2,
                    shopList: [],
                    isMore: false
                })
            }
            this.data.isAjax = false
      if(!Storage.getShopId()){
        Storage.setShopId(res.data.rows[0].id)
      }
    }).catch((res) => {
      if (this.data.params.page > 1) {
        this.data.currentPage--
      }
      this.data.isAjax = false
    })
  },
  onScroll() { // 向下滑动的时候请求数据
    this.data.loadMoreView.loadMore()
  },
  _loadMoreListener(){
      if (this.data.isAjax) return
      let page = this.data.currentPage
      page += 1
      let {params} = this.data
      params.pageNum = page

      this.data.params = params
      this.data.currentPage = page
      this.getShopList(this.data.params)
  },
  onSearch(e) {
    this.setData({
      name: e.detail
    })
    this.getShopList()
  },
  toAdd() {
    // this.setData({
    //   show: true
    // })
    Tool.navigateTo('/pages/mine/shop/add-shop/add-shop')
  },
  onConfirm() {
    if (Tool.isEmptyStr(this.data.shopName)){
      Tool.showToast("店铺名称不能为空");
      return
    }
    let params = {
      shopName: this.data.shopName,
    }
    API.createShop(params).then((res) => {
      Tool.redirectTo('/pages/mine/shop/shop')
    }).catch((res) => {
      console.error(res)
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  toShopUser(e) {
    const shopId = e.currentTarget.dataset.id
    this.selectComponent('#mySwipe').close()
    Tool.navigateTo(`/pages/mine/shop/user/user?shopId=${shopId}`)
  },
  selectShop(e) {
    let shopId = e.currentTarget.dataset.id
    this.selectComponent('#mySwipe').close()
    API.setShopCache({
      shopId: shopId
    }).then(() => {
      wx.showToast({
        title: '当前店铺已切换为：' + e.currentTarget.dataset.name,
        icon: 'none',
        duration: 1500,
        success: () => {
          Storage.setShopId(shopId)
          setTimeout(() => {
            Tool.switchTab('/pages/mine/mine')
          }, 1500)
        }
      })
    }).catch(() =>{
      //
    })
  },
})
